package de.unibremen.mum.app.whatscool;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class ResultFragment extends Fragment {

	View resultView;
	DatabaseHandler db;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		db = new DatabaseHandler(getActivity());
		resultView = inflater.inflate(R.layout.activity_result, container, false);
		int id = 34;

		TextView ueberschrift = (TextView) resultView.findViewById(R.id.textView1);
		TextView kommentar = (TextView) resultView.findViewById(R.id.textView2);
		RatingBar r1 = (RatingBar) resultView.findViewById(R.id.ratingBarResult);

		RatingClass rating = db.getRating(id);
		ueberschrift.setText(rating.getName());
		kommentar.setText(rating.getComment());
		r1.setRating(Float.parseFloat(rating.getResult()));
		createProductPic(rating.getPic());
		return resultView;

	}

	/**
	 * Erzeugt das Foto in der View
	 * @param url URL zum Foto
	 */
	public void createProductPic(String url) {
		if ((new File(url)).exists()) {
			Bitmap bitmap = BitmapFactory.decodeFile(url);
			if (bitmap != null) {
				ImageView iv = (ImageView) resultView.findViewById(R.id.quickContactBadge1);
				iv.setImageBitmap(bitmap);
			} else {
				Toast.makeText(getActivity(), "Foto nicht gefunden", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
